package com.vinci.wishDemo.controller;

import com.vinci.util.Constants;
import com.vinci.util.JsonObject;
import com.vinci.wishDemo.entity.TbContentAppreciate;
import com.vinci.wishDemo.entity.TbContentWishList;
import com.vinci.wishDemo.mapper.TbContentWishListMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vinci.wishDemo.service.TbContentWishListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 心愿单表 前端控制器
 * </p>
 *
 * @author 郭三木
 * @since 2021-12-10
 */
@RestController
@RequestMapping("/wishDemo")
@Slf4j
public class TbContentWishListController {
    @Resource
    private TbContentWishListMapper wishListMapper;
    @Autowired
    private TbContentWishListService wishListService;
    @Autowired
    private RedisTemplate redisTemplate;

    //查询所有心愿单
    @RequestMapping("/queryWishList")
    public String queryWishList(String createBy, Date beginTime, Date endTime) {
        QueryWrapper<TbContentWishList> queryWrapper = new QueryWrapper<>();

        //如果创建人不为空则执行一下语句
        if (createBy != null && createBy != "") {
            queryWrapper.eq("create_by", createBy);
//            List<TbContentWishList> users = wishListMapper.selectList(queryWrapper);
            //参数一：当前页; 参数二：页面大小
            Page<TbContentWishList> page = new Page<>(1, 2);
            IPage<TbContentWishList> userIPage = wishListMapper.selectPage(page, queryWrapper);
            page.getRecords().forEach(System.out::println);
            System.out.println(page.getTotal());    //打印总数
            return "ok";

            //如果开始时间与结束时间不为空则执行以下语句
        } else if (beginTime != null && endTime != null) {
            queryWrapper.ge("create_time", beginTime).le("create_time", endTime);
//            List<TbContentWishList> users = wishListMapper.selectList(queryWrapper);
            //参数一：当前页; 参数二：页面大小
            Page<TbContentWishList> page = new Page<>(1, 2);
            IPage<TbContentWishList> userIPage = wishListMapper.selectPage(page, queryWrapper);
            page.getRecords().forEach(System.out::println);
            System.out.println(page.getTotal());    //打印总数
            return "ok";

            //如果创建人，开始时间结束时间都不是空
        } else if (createBy != null && createBy != "" && beginTime != null && endTime != null) {
            queryWrapper.eq("create_by", createBy).ge("createTime", beginTime).le("createTime", endTime);
            //参数一：当前页; 参数二：页面大小
            Page<TbContentWishList> page = new Page<>(1, 2);
            IPage<TbContentWishList> userIPage = wishListMapper.selectPage(page, queryWrapper);
            page.getRecords().forEach(System.out::println);
            System.out.println(page.getTotal());    //打印总数
        }

        //没有传参则查询全部
        //参数一：当前页; 参数二：页面大小
        Page<TbContentWishList> page = new Page<>(1, 2);
        IPage<TbContentWishList> userIPage = wishListMapper.selectPage(page, null);
        page.getRecords().forEach(System.out::println);
        System.out.println(page.getTotal());    //打印总数
        return "ok";
    }

    /*
    条件查询
     */
    @GetMapping("/queryList")
    public JsonObject queryList(String createBy, String beginTime, String endTime) {
        //创建人、开始时间、结束时间
        List<TbContentWishList> list = wishListService.querylist(createBy, beginTime, endTime);
        log.info("这是log4j2的日志测试，info级别");
        log.warn("这是log4j2的日志测试，warn级别");
        log.error("这是log4j2的日志测试，error级别");
        log.info(String.valueOf(list));
        JsonObject object = new JsonObject();
        object.setCode(Constants.OK_COOE);
        object.setMsg("ok");
        object.setCount(list.size());
        object.setData(list);

        return object;
    }

    /*
    回复功能
     */
    @PostMapping("/replyWish")
    public JsonObject replyWish(HttpSession session, String userId, String wishId, String replyContent, Integer wishStats) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String format = dateFormat.format(date);
        //回复时间，回复心愿id，回复内容，是否达成
        wishListService.replyWish(userId, format, wishId, replyContent, wishStats);
        JsonObject object = new JsonObject();
        object.setCode(Constants.OK_COOE);
        object.setMsg("ok");
        return object;
    }

    /*
    删除心愿
     */
    @GetMapping("/delwish")
    public JsonObject delwish(String wishId, Model model) {
        JsonObject object = new JsonObject();
        if (wishId == null){
            object.setCode(Constants.FAIL_COOE);
            object.setMsg("不能为空");
            return object;
        }
        wishListService.delwish(wishId);
        //获取当前状态返回给前台
        TbContentWishList wishstats = wishListService.querystats(wishId);
        model.addAttribute("wishstats",wishstats);
        object.setCode(Constants.OK_COOE);
        object.setMsg("ok");
        return object;
    }

    //根据发表人查询心愿单
    @RequestMapping("/queryPerList")
    public String queryPerList(String createBy) {
        QueryWrapper<TbContentWishList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_by", createBy);
        List<TbContentWishList> users = wishListMapper.selectList(queryWrapper);
        return "ok";
    }

    //查询时间段内的心愿单
    @RequestMapping("/querytimeList")
    public String querytimeList(Date beginTime, Date endTime) {
        QueryWrapper<TbContentWishList> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("createTime", beginTime).le("createTime", endTime);
        List<TbContentWishList> users = wishListMapper.selectList(queryWrapper);
        return "ok";
    }

    @RequestMapping("/pagelist/{num}/{size}")
    public IPage<TbContentWishList> pagelist(@PathVariable("num") int num,
                                             @PathVariable("size") int size) {
        Page<TbContentWishList> page = new Page<>(num, size);
        return wishListService.selectPageVo(page);
    }

    /*
    redis测试
     */
    @GetMapping("/emp/{wishId}")
    public Object getWishById(@PathVariable("wishId")String wishId){
        Object wi = redisTemplate.opsForValue().get(wishId);
        //从缓存里获取数据，如果缓存中有则直接返回
        //如果无，则查询mysql，并将数据设置到缓存
        if (wi == null){
            System.out.println("查询数据库执行");
            TbContentWishList tbContentWishList = wishListMapper.selectById(wishId);
            redisTemplate.opsForValue().set(wishId,tbContentWishList);
        }else{
            System.out.println("缓存执行");
        }

        return wi;
    }

}

